package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import lombok.Builder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service
public class DishServiceImpl implements DishService {
    @Autowired
    DishMapper dishMapper;
    @Autowired
    DishFlavorMapper dishFlavorMapper;
    @Autowired
    SetmealDishMapper setmealDishMapper;

    /**
     * 添加菜品
     * 菜品与口味 为一对多关系
     *
     * @param dishDTO
     */
    @Transactional//要么完成整个操作，要么回滚
    @Override
    public void DishWishFlavor(DishDTO dishDTO) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO, dish);

        //在菜品中添加一条数据
        dishMapper.addDish(dish);

        Long dishId = dish.getId();

        //在口味中添加多条数据
        List<DishFlavor> flavors = dishDTO.getFlavors();
        if (flavors != null && flavors.size() > 0) {
            for (DishFlavor flavor : flavors) {
                flavor.setDishId(dishId);
            }
            dishFlavorMapper.insertBatch(flavors);
        }
    }

    /**
     * 菜品分页查询
     *
     * @param queryDTO
     * @return
     */
    @Override
    public PageResult page(DishPageQueryDTO queryDTO) {
        PageHelper.startPage(queryDTO.getPage(), queryDTO.getPageSize());
        Page<DishVO> dishVOS = dishMapper.queryPage(queryDTO);
        return new PageResult(dishVOS.getTotal(), dishVOS.getResult());
    }

    /**
     * 菜品的批量删除
     *
     * @param ids
     */
    @Transactional
    @Override
    public void delBatch(List<Long> ids) {
        //判断当前菜品是否在起售中
        for (Long id : ids) {
            Dish dish = dishMapper.getById(id);
            if (dish.getStatus() == StatusConstant.ENABLE) {
                throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
            }
        }

        //判断菜品是否与套餐关联
        List<Long> setmealIdsByDishIds = setmealDishMapper.getSetmealIdsByDishIds(ids);
        if (setmealIdsByDishIds != null && setmealIdsByDishIds.size() > 0) {
            throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
        }

        //删除 菜品和对应的口味
        /*法一:for (Long id : ids) {
            dishMapper.delByDishId(id);
            dishFlavorMapper.delDishFlavor(id);
        }*/
        //法二:
        //删除 主键菜品
        dishMapper.delByDishIds(ids);

        //删除菜品对应的口味
        dishFlavorMapper.delDishFlavors(ids);


    }

    /**
     * 根据id查询菜品信息和对应的口味数据
     *
     * @param id
     * @return
     */
    @Override
    public DishVO getByIdWithFlavor(Long id) {
        DishVO dishVO = new DishVO();

        //在dish列表中查询菜品信息
        Dish dishById = dishMapper.getById(id);
        //利用属性拷贝将 菜品信息拷贝到vo
        BeanUtils.copyProperties(dishById, dishVO);

        //根据菜品id 查询对应的口味
        List<DishFlavor> dishFlavorList = dishFlavorMapper.getFlavorsByDishId(id);
        dishVO.setFlavors(dishFlavorList);
        return dishVO;
    }

    /**
     * 修改菜品id的信息和对应的口味
     *
     * @param dishDTO
     */
    @Transactional
    @Override
    public void updateByIdWithFlavor(DishDTO dishDTO) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO, dish);

        //更新dish根据菜品id更新信息
        dishMapper.updateById(dish);

        //更新对应菜品的口味=删除原有+添加现有
        dishFlavorMapper.delDishFlavor(dishDTO.getId());//删除菜品对应的所有口味

        List<DishFlavor> flavors = dishDTO.getFlavors();
        if (flavors != null && flavors.size() > 0) {
            for (DishFlavor flavor : flavors) {
                flavor.setDishId(dishDTO.getId());
            }
            dishFlavorMapper.insertBatch(flavors);
        }
    }

    /**
     * 根据分类id查询菜品
     *
     * @param categoryId
     * @return
     */
    @Builder
    @Override
    public List<Dish> GetDishsByCategoryId(Long categoryId) {
        Dish dish = Dish.builder().
                status(StatusConstant.ENABLE).
                categoryId(categoryId).
                build();
        log.error(dish.toString());
        return dishMapper.GetDishsByCategoryId(dish);
    }

    /**
     * 修改菜品状态
     *
     * @param status
     * @param id
     */
    @Override
    public void UpdateDishStatus(Integer status, Long id) {
        Dish dish = Dish.builder()
                .id(id)
                .status(status).build();
        log.error(dish.toString());
        dishMapper.updateById(dish);
    }

    /**
     * 条件查询菜品和口味
     * @param dish
     * @return
     */
    public List<DishVO> listWithFlavor(Dish dish) {
        List<Dish> dishList = dishMapper.GetDishsByCategoryId(dish);

        List<DishVO> dishVOList = new ArrayList<>();

        for (Dish d : dishList) {
            DishVO dishVO = new DishVO();
            BeanUtils.copyProperties(d,dishVO);

            //根据菜品id查询对应的口味
            List<DishFlavor> flavors = dishFlavorMapper.getFlavorsByDishId(d.getId());

            dishVO.setFlavors(flavors);
            dishVOList.add(dishVO);
        }
        return dishVOList;
    }

}
